package com.cloudkinto.controller.address;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.address.AddressBookAddBo;
import com.cloudkinto.bo.address.AddressBookQueryBo;
import com.cloudkinto.bo.address.AddressBookUpdateBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.entity.AddressBookDo;
import com.cloudkinto.service.address.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 地址簿
 *
 * @author long
 * @since 2025-01-06
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "地址簿模块")
@RequestMapping("/api/admin/addressBook")
public class AddressBookOmsController extends BaseController {
    private final AddressBookService addressBookService;

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "地址簿新增", notes = "地址簿新增")
    public SingleResult add(@RequestBody @Validated AddressBookAddBo req) {
        return SingleResult.success("新增成功", addressBookService.add(req));
    }

    @UserSessionRequired
    @PostMapping("/checkExist")
    @ApiOperation(value = "地址簿验证存在，存在返回id", notes = "地址簿验证存在，存在返回id")
    public SingleResult<Long> checkExist(@RequestBody @Validated AddressBookAddBo bo) {
        return SingleResult.success("操作成功", addressBookService.checkExist(bo, getTenantId(), getCompanyId()));
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "地址簿修改", notes = "地址簿修改")
    public SingleResult update(@RequestBody @Validated AddressBookUpdateBo req) {
        return SingleResult.success("修改成功", addressBookService.update(req));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "地址簿根据id删除", notes = "地址簿根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        return SingleResult.success("删除成功", addressBookService.delete(id));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "地址簿根据id查看详情", notes = "地址簿根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "请求成功", response = AddressBookDo.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", addressBookService.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "地址簿查询初始化", notes = "地址簿查询初始化")
    public SingleResult pageInit() {
        return addressBookService.pageInit(getTenantId(), getCompanyId());
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "地址簿查询（分页）", notes = "地址簿查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "请求成功", response = AddressBookDo.class))
    public PageResult listPage(@RequestBody @Validated AddressBookQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setCompanyId(getCompanyId());
        return addressBookService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("/listPageStatusSta")
    @ApiOperation(value = "地址簿查询状态统计", notes = "地址簿查询状态统计")
    public SingleResult listPageStatusSta(@RequestBody @Validated AddressBookQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setCompanyId(getCompanyId());
        bo.setAddressType(null);
        return addressBookService.listPageStatusSta(bo);
    }

    @UserSessionRequired
    @GetMapping("/getAddressBookImportTemplate")
    @ApiOperation(value = "获取模板", notes = "获取模板")
    public void getAddressBookImportTemplate(HttpServletResponse response) throws IOException {
        addressBookService.getAddressBookImportTemplate(response);
    }

    @UserSessionRequired
    @PostMapping("/importAdd")
    @ApiOperation(value = "导入地址簿", notes = "导入地址簿")
    public SingleResult importAdd(MultipartFile file) {
        return SingleResult.success(addressBookService.importAdd(file));
    }
}

